/*
 * @Author: 小花
 * @Date: 2022-01-18 19:29:09
 * @LastEditors: 小花
 * @LastEditTime: 2022-05-14 22:47:19
 * @Description: 
 */
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'

import wxScanLogin from '@/components/wxScanLogin/wxScanLogin.vue'
import { ElMessage } from 'element-plus'

const routes = [
  { path: '/', component: wxScanLogin, name: 'wxScanLogin' },
  { path: '/newUser', component: () => import('@/components/newUser/newUser.vue'), name: 'newUser' },
  { path: '/userHome', redirect: "/userHome/user_repairCenter", name: 'user' },
  {
    path: '/userHome', component: () => import('@/components/userHome/userHome.vue'), meta: { auth: true, type: 'user' }, children: [
      { path: 'user_repairCenter', component: () => import('@/components/userHome/RepairCenter/repairCenter.vue'), name: 'user_repairCenter', meta: { auth: true, type: 'user' } },
      { path: 'user_repairRecord', component: () => import('@/components/userHome/repairRecord/repairRecord.vue'), name: 'repairRecord', meta: { auth: true, type: 'user' } },
      { path: 'charge_on', component: () => import('@/components/userHome/repairRecord/chargeOn/chargeOn.vue'), name: 'charge_on', meta: { auth: true, type: 'user' } },
    ]
  },
  { path: '/workerHome', redirect: "/workerHome/worker_repairCenter", name: 'worker' },
  {
    path: '/workerHome', component: () => import('@/components/workerHome/workerHome.vue'), meta: { auth: true, type: 'worker' }, children: [
      { path: 'worker_repairCenter', component: () => import('@/components/workerHome/repairCenter/repairCenter.vue'), name: 'worker_repairCenter', meta: { auth: true, type: 'worker' } },
      { path: 'worker_repairRecord', component: () => import('@/components/workerHome/repairRecord/repairRecord.vue'), name: 'worker_repairRecord', meta: { auth: true, type: 'worker' } },
      { path: 'record_details', component: () => import('@/components/workerHome/repairRecord/recordDetails/recordDetails.vue'), name: 'record_details', meta: { auth: true, type: 'worker' } },
    ]
  }
]
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.auth && !localStorage.getItem("satoken")) {
    alert("请先行登录系统")
    next({ name: 'wxScanLogin' })
    return
  }
  const loginType = localStorage.getItem('userInfo') && JSON.parse(localStorage.getItem('userInfo')).loginType
  if (to.meta.type !== loginType && to.meta.type) {
    ElMessage.error('暂无权限')
    router.go(-1)
    return
  }
  next();
})

export default router